/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nct.model;

import org.nct.model.entities.Chude;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.nct.model.entities.Tintuc;

/**
 *
 * @author haott
 */
public class chuDeProvider implements IModel {

    static final Logger logger = Logger.getLogger(chuDeProvider.class.toString());

    @Override
    public List<Object> SelectAll() {
        List<Object> lists;
        try {
            DataProvider.getInstance().Open();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        //Object objCache = cache.getInstance().getValue("selectAll");
        logger.info("Select all");

        lists = new ArrayList<>();
        String query = "SELECT * FROM CHUDE";

        ResultSet res = DataProvider.getInstance().ExecuteReader(query);
        try {
            while (res.next()) {
                lists.add(new Chude(res.getInt("ID"), res.getString("TENCHUDE")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lists;
    }

    @Override
    public Object SelectOne(Object id) {

        Chude result = null;

        PreparedStatement selectPST = null;

        String query = "SELECT * FROM CHUDE WHERE ID = ?";
        try {
            selectPST = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            selectPST.setInt(1, Integer.parseInt(id.toString()));
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        ResultSet res = DataProvider.getInstance().ExecuteReaderPreparedStatement();
        try {
            while (res.next()) {
                result = new Chude(res.getInt(1), res.getString(2));
            }
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    @Override
    public int Insert(Object obj) {

        int count;
        PreparedStatement statement = null;

        String query = "INSERT CHUDE(ID,TENCHUDE,ID_TINTUC) VALUES(?,?,?)";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            Chude _cd = (Chude) obj;
            statement.setInt(1, _cd.getId());
            statement.setString(2, _cd.getTenchude());
            if (_cd.getTintucList() !=null) {
                statement.setInt(3, _cd.getTintucList().get(0).getId());
            }
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;
    }

    public List<Chude> GetListChuDeFromTinTuc(int _intIdTinTuc) {
        List<Chude> lists = null;

        PreparedStatement selectPST = null;

        String query = "SELECT * FROM CHUDE WHERE ID_TINTUC = ?";
        try {
            selectPST = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            selectPST.setInt(1, _intIdTinTuc);
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        ResultSet res = DataProvider.getInstance().ExecuteReaderPreparedStatement();

        try {
            while (res.next()) {
                lists.add(new Chude(res.getInt("ID"), res.getString("TENCHUDE")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return lists;

    }

    @Override
    public int Update(Object id, Object obj) {

        int count;
        PreparedStatement statement = null;

        // UPDATE `CHUDE` SET `ID`=[value-1],`TENCHUDE`=[value-2] WHERE 1
        logger.info("Update database");

        String query = "UPDATE CHUDE SET ID=?,TENCHUDE=? WHERE ID=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            Chude _cd = (Chude) obj;
            statement.setInt(1, _cd.getId());
            statement.setString(2, _cd.getTenchude());
            statement.setInt(3, Integer.parseInt(id.toString()));

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;
    }

    @Override
    public int Delete(Object id) {
        int count;
        PreparedStatement statement = null;

        String query = "DELETE FROM CHUDE WHERE ID=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            statement.setInt(1, Integer.parseInt(id.toString()));

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;

    }
    
    public int DeleteFromTinTuc(int id) {
        int count;
        PreparedStatement statement = null;

        String query = "DELETE FROM CHUDE WHERE ID_TINTUC=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            statement.setInt(1, id);

        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;

    }

    @Override
    public int increase(Object obj) {
        // change obj.ID to Max+1         
        ((Chude) obj).setId(getMaxID() + 1);

        return Insert(obj);
    }

    @Override
    public int getMaxID() {
        int maxID = 0;
        try {
            DataProvider.getInstance().Open();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        String query = "SELECT MAX(ID) FROM CHUDE";

        ResultSet res = DataProvider.getInstance().ExecuteReader(query);

        try {
            if (res.next()) {
                maxID = res.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(chuDeProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return maxID;
    }
}
